package org.shop.service.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import org.shop.common.MyConstant;
import org.shop.entity.LoginRecord;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.LoginRecordService;
import org.shop.util.DateUtils;
import org.shop.util.MD5Util;
import org.shop.util.ObjUtil;
@Service
public class SecurityProvider implements AuthenticationProvider {
    @Autowired
    private UserDetailsServiceImpl userDetailsServiceImpl;
	@Autowired
	private LoginRecordService loginRecordService;
    @Override
    public Authentication authenticate(Authentication authentication) {

        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        MyUserDetail userDetails = (MyUserDetail) userDetailsServiceImpl.loadUserByUsername(token.getName());
        WebAuthenticationDetails details=(WebAuthenticationDetails) token.getDetails();
        details.getRemoteAddress();
    	Map<String,Object> paramMap = new HashMap<String,Object>();
		DateUtils du = new DateUtils();
		//获取登录人用户信息
  		paramMap.put("userId", userDetails.getUserId());
  		paramMap.put("endTime", du.getLastYear());
  		loginRecordService.delLoginRecordOfYearByUserId(paramMap);
  		LoginRecord loginRecordInfo = new LoginRecord();
  		loginRecordInfo.setUserId(userDetails.getUserId());
  		loginRecordInfo.setLoginIp(details.getRemoteAddress());
  		if(userDetails.getUserType() == MyConstant.USER_TYPE_SHOP) {
			loginRecordInfo.setLoginRemark(userDetails.getUsername()+"商家登录");
			loginRecordInfo.setLoginType((byte) 1);
			loginRecordInfo.setLoginSrc((byte) 1);
			loginRecordService.insertSelective(loginRecordInfo);
		}else if(userDetails.getUserType() == MyConstant.USER_TYPE_ADMIN){
			loginRecordInfo.setLoginRemark(userDetails.getUsername()+"管理员登录登录");
			loginRecordInfo.setLoginType((byte) 2);
			loginRecordInfo.setLoginSrc((byte) 1);
			loginRecordService.insertSelective(loginRecordInfo);
		}else if(userDetails.getUserType() == MyConstant.USER_TYPE_SUPPLIER) {
			loginRecordInfo.setLoginRemark(userDetails.getUsername()+"供应商登录");
			loginRecordInfo.setLoginType((byte) 0);
			loginRecordInfo.setLoginSrc((byte) 2);
			loginRecordService.insertSelective(loginRecordInfo);
		}else if(userDetails.getUserType() == MyConstant.USER_TYPE_AUTOTROPHY) {
			loginRecordInfo.setLoginRemark(userDetails.getUsername()+"自营登录");
			loginRecordInfo.setLoginType((byte) 0);
			loginRecordInfo.setLoginSrc((byte) 2);
			loginRecordService.insertSelective(loginRecordInfo);
		}else if(userDetails.getUserType() == MyConstant.USER_TYPE_LIVEMANAGER){
			loginRecordInfo.setLoginRemark(userDetails.getUsername()+"生活管理员登录");
			loginRecordInfo.setLoginType((byte) 0);
			loginRecordInfo.setLoginSrc((byte) 2);
			loginRecordService.insertSelective(loginRecordInfo);
		}else if(userDetails.getUserType() == MyConstant.USER_TYPE_BBSMANAGER){
			loginRecordInfo.setLoginRemark(userDetails.getUsername()+"论坛管理员登录");
			loginRecordInfo.setLoginType((byte) 0);
			loginRecordInfo.setLoginSrc((byte) 2);
			loginRecordService.insertSelective(loginRecordInfo);
		}else {
			loginRecordInfo.setLoginRemark(userDetails.getUsername()+"app登录");
			loginRecordInfo.setLoginType((byte) 0);
			loginRecordInfo.setLoginSrc((byte) 2);
			loginRecordService.insertSelective(loginRecordInfo);
		}
  		if (ObjUtil.isNullOrEmpty(userDetails)) {
  			throw new UsernameNotFoundException("找不到该用户");
  		}
        if(!userDetails.getPassword().equals(MD5Util.MD5(token.getCredentials().toString())))
        {
              throw new BadCredentialsException("密码错误");
        }
        return new UsernamePasswordAuthenticationToken(userDetails,userDetails.getPassword(),userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
    	
        return UsernamePasswordAuthenticationToken.class.equals(authentication);
    }

}
